#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>  
#include <string.h>
#include <sys/wait.h>
int main()
{
  int fd = open("./test.txt",O_RDWR | O_CREAT, 0777);
  if(fd < 0){
    perror("open erroe");
  }

  char *ptr = "天黑了~~\n";
  int ret = write(fd, ptr, strlen(ptr));
  if(ret < 0){
    perror("write error");
  }
  //跳转文件读取位置
  lseek(fd, 0, SEEK_SET);

  char buf[1024] = {0};
  ret = read(fd, buf, sizeof(buf) - 1);
  if(ret < 0 ){
    perror("read error");
    return -1;
  } else if(ret == 0){
    printf("at end of file\n");
    return -1;
  }else{
    printf("%d\n", ret);
  }
  close(fd);
  return 0;
}
